package com.cinnamon.rss.service;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.stereotype.Service;

import com.cinnamon.rss.model.Item;

@Service
public class DateConverterService {
	
	public DateConverterService() {
		
	}
	
	private long getDateDiff(long dateFrom, TimeUnit timeUnit) {
	    long diffInMillies = System.currentTimeMillis() - dateFrom;
	    return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
	}
	
	public List<Item> converteItems(List<Item> items) {
		long dateDiff = 0;
		long temp = 0;
		String mark = "";
		for (Item item: items) {
			long dateFrom = item.getPubDate();
			if ((temp = getDateDiff(dateFrom, TimeUnit.DAYS)) > 0) {
				dateDiff = temp;
				mark = "d";
			} else if ((temp = getDateDiff(dateFrom, TimeUnit.HOURS)) > 0) {
				dateDiff = temp;
				mark = "h";
			} else if ((temp = getDateDiff(dateFrom, TimeUnit.MINUTES)) > 0) {
				dateDiff = temp;
				mark = "min";
			} else {
				dateDiff = temp;
				mark = "sec";
			}
			item.setPubDate(dateDiff);
			item.setDateMark(mark);
		}
		return items;
	}
}
